package com.servlet;


import com.daofactory.DaoFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
 * @author 徐洲
 * @version 1.0
 */
public class CategoryServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        HttpSession session = request.getSession();
        if ("add".equals(action)){
            String category_name = request.getParameter("category_name");
            if (DaoFactory.getCategoryDAOInstance().addCategory(category_name)){

            }else {

            }
        } else if ("delete".equals(action)) {
            String category_id = request.getParameter("category_id");
//            System.out.println(category_id);
            if (DaoFactory.getCategoryDAOInstance().deleteCategory(category_id)){

            }else {

            }
        } else if ("update".equals(action)) {
            String category_id = request.getParameter("category_id");
            String category_name = request.getParameter("category_name");
            if (DaoFactory.getCategoryDAOInstance().updateCategory(category_name, category_id)){

            }else {

            }
        }

        response.sendRedirect("./admin_category.jsp");
    }
}
